import {
    FindLevelCardList,
    EnableLevelCard,
    DisabledLevelCard,
    AddLevelCard,
    FindLevelCardById,
    UpdateLevelCard,
    DelLevelCard,
    FindLevelList,
    GetPointListWithPage,
    FindStorePoint,
    SetStorePoint,
    AddPointRule,
    DeleteStorePoint,
    FindPointRule,
    UpdatePointRule,
    GetCouponList
} from '@/services/member/gradeCard';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'gradeCard',

    state: {
        levelCardList: [],
        LevelCardById: [],
        levelList: [],
        pointList: [],
        storePointList: [],
        pointRule: [],
        couponList:[]
    },

    effects: {

        // 获取等级卡列表
        *FindLevelCardList({payload, callBack}, {call, put}) {
            const data = yield call(FindLevelCardList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveFindLevelCardList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },


        // ID获取数据
        *FindLevelCardById({payload, callBack}, {call, put}) {
            const data = yield call(FindLevelCardById, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveFindLevelCardById',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        //Id获取规则信息
        *FindPointRule({payload, callBack}, {call, put}) {
            const data = yield call(FindPointRule, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveFindPointRule',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        // 积分分页
        *GetPointListWithPage({payload, callBack}, {call, put}) {
            const data = yield call(GetPointListWithPage, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetPointListWithPage',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        //商铺积分
        *FindStorePoint({payload, callBack}, {call, put}) {
            const data = yield call(FindStorePoint, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveFindStorePoint',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },


        // 获取等级
        *FindLevelList({payload, callBack}, {call, put}) {
            const data = yield call(FindLevelList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveFindLevelList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

           // 获取优惠券列表
           *GetCouponList({payload, callBack}, {call, put}) {
            const data = yield call(GetCouponList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetCouponList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        *EnableLevelCard({payload, callBack}, {call, put}) {
          const data = yield call(EnableLevelCard, payload);
          yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })          
        },

        *DisabledLevelCard({payload, callBack}, {call, put}) {
          const data = yield call(DisabledLevelCard, payload);
          yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })         
        },

        // 新建
        *AddLevelCard({payload, callBack}, {call, put}) {
            const data = yield call(AddLevelCard, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

         // 修改
        *UpdateLevelCard({payload, callBack}, {call, put}) {
          const data = yield call(UpdateLevelCard, payload);
          yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })          
        },

        //设置积分规则
        *SetStorePoint({payload, callBack}, {call, put}) {
            const data = yield call(SetStorePoint, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

          // 新增奖励积分规则
          *AddPointRule({payload, callBack}, {call, put}) {
            const data = yield call(AddPointRule, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

          // 编辑奖励积分规则
          *UpdatePointRule({payload, callBack}, {call, put}) {
            const data = yield call(UpdatePointRule, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },


        //删除
        *DelLevelCard({payload, callBack}, {call, put}) {
            const data = yield call(DelLevelCard, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

        //删除积分规则
          *DeleteStorePoint({payload, callBack}, {call, put}) {
            const data = yield call(DeleteStorePoint, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

    },

    reducers: {
        saveFindLevelCardList(state, {payload}) {
            return {
                ...state,
                levelCardList: payload.dataList,
            };
        },

        saveFindLevelCardById(state, {payload}) {
            return {
                ...state,
                LevelCardById: payload.dataList,
            };
        },


        saveFindLevelList(state, {payload}) {
            return {
                ...state,
                levelList: payload.dataList,
            };
        },

        saveGetPointListWithPage(state, {payload}) {
            return {
                ...state,
                pointList: payload.dataList,
            };
        },

        saveFindStorePoint(state, {payload}) {
            return {
                ...state,
                storePointList: payload.dataList,
            };
        },

        saveFindPointRule(state, {payload}) {
            return {
                ...state,
                pointRule: payload.dataList,
            };
        },

        saveGetCouponList(state, {payload}) {
            return {
                ...state,
                couponList: payload.dataList,
            };
        },
    }
}
